<?php

namespace app\controllers;

use app\models\AccountMoney;
use app\models\Express;
use app\models\Income;
use app\service\InlandService;
use yii;
use app\lib\BaseFunction;
use app\models\Bank_bill;
use app\models\ModelFactory;
use app\models\Staff;
use app\models\Baoxiao;
use yii\data\Pagination;

//财务管理模块

class FinanceController extends CommonController
{
    public $layout = false;


    //结算中心-外部账单
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionOutorder()
    {
        //所有业务员
        $model = new \app\models\Staff();
        $staff_list = $model->get_staff_list();
        //快递express_id
        $express_model = new \app\models\Express();
        $express_type = $express_model->get_express_type();
        $express = [];
        foreach ($express_type as $k => $v) {
            $express[$v['id']] = $v['name'];
        }
        $staff = [];
        foreach ($staff_list as $k => $v) {
            $staff[$v['job_number']] = $v['name'];
        }
        yii::$app->view->params['express_list'] = json_encode($express);
        yii::$app->view->params['staff_list'] = json_encode($staff);
        return $this->render('outorder');
    }

    //快递excel上传
    public function actionUpload_express_excel()
    {
        $file = $_FILES['file'];
        $suffix = explode('.', $file['name']);
        if (!($suffix[1] == 'xls' || $suffix[1] == 'xlsx')) {
            return json_encode(['code' => 0, 'msg' => '不是excel文件']);
        }

        $path = $this->get_folder();
        $real_path = $path . DIRECTORY_SEPARATOR . 'express_' . time() . "." . $suffix[1];
        move_uploaded_file($file['tmp_name'], $real_path);

        $objPHPExcel = $this->get_excel($real_path, $suffix[1]);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

        //customer_list
        $model = new \app\models\Customer();
        $customer_list = $model->get_all_customer();
        //从第二行开始读取
        for ($j = 2; $j <= $highestRow; $j++) {
            $date = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
            if ($date) {
                if (is_string($date)) {
                    $data[$j][] = $date;//日期
                } else {
                    $data[$j][] = date('Y-m-d', ($objPHPExcel->getActiveSheet()->getCell("A$j")->getValue() - 25569) * 24 * 60 * 60);//date
                }

                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();//快递公司
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();//业务员工号
                //基于客户名查找客户编号
                $customer_name = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
                if ($customer_name) {
                    $tmp = 0;
                    foreach ($customer_list as $k => $v) {
                        if ($v['customer_name'] == $customer_name) {
                            $tmp = 1;
                            $data[$j][] = $v['number'];
                            $data[$j][] = $customer_name;
                            break;
                        }
                    }
                    if (!$tmp) {
                        $data[$j][] = "";
                        $data[$j][] = $customer_name;
                    }
                } else {
                    $data[$j][] = "";
                    $data[$j][] = "";
                }

                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("F$j")->getValue();//invioce
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("G$j")->getValue();//发往国家
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("H$j")->getValue();//发件渠道
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("I$j")->getValue();//单号
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("J$j")->getValue();//件数
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("K$j")->getValue();//重量
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("L$j")->getValue();//重量
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("M$j")->getValue();//单价
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("N$j")->getValue();//金额
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("O$j")->getValue();//运费
            }
        }
        foreach ($data as $k => $v) {
            if (!$v[0]) {
                unset($data[$k]);
            }
        }
        $db = new Express();
        $re = $db->insert_bacth_express_fee($data);
        $db->updatePid();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    public function actionExpress_add()
    {
        $data = $_POST['data'];
        foreach ($data as $k => $v) {
            if (!$v[0]) {
                unset($data[$k]);
            }
        }
        $keys = [
            'date', 'express_id', 'name', 'customer_number', 'customer_name', 'invoice_order',
            'country', 'express_channel', 'tradeno', 'to_tradeno', 'num', 'weight',
            'per_kg', 'fee', 'remark'
        ];
        $re = yii::$app->db->createCommand()
            ->batchInsert(yii::$app->db->tablePrefix . "express", $keys, $data)->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    //仓库费用excel上传
    public function actionUpload_repo_fee_excel()
    {
        $file = $_FILES['repo'];
        $suffix = explode('.', $file['name']);
        if (!($suffix[1] == 'xls' || $suffix[1] == 'xlsx')) {
            echo json_encode(['code' => 0, 'msg' => '不是excel文件']);
            return false;
        }

        $path = $this->get_folder();
        $real_path = $path . DIRECTORY_SEPARATOR . 'repo_' . time() . "." . $suffix[1];
        move_uploaded_file($file['tmp_name'], $real_path);

        //customer_list
        $model = new \app\models\Customer();
        $customer_list = $model->get_all_customer();

        $objPHPExcel = $this->get_excel($real_path, $suffix[1]);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        for ($j = 2; $j <= $highestRow; $j++) {
            $date = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
            if ($date) {
                if (is_string($date)) {
                    $data[$j][] = $date;
                } else {
                    $data[$j][] = date('Y-m-d', ((int)$objPHPExcel->getActiveSheet()->getCell("A$j")->getValue() - 25569) * 24 * 60 * 60);//date
                }
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("F$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("G$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("H$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("I$j")->getCalculatedValue();
                $customer_name = (string)$objPHPExcel->getActiveSheet()->getCell("K$j")->getValue();
                //基于客户名查找客户编号
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell('J' . $j)->getValue();
                $data[$j][] = $customer_name;
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("L$j")->getValue();
                $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("M$j")->getValue();//remark
            }
        }
        //var_dump($data);exit;
        $keys = [
            'date', 'name', 'product', 'product_number', 'package', 'carton', 'pay',
            'fee', 'pay_all', 'num', 'customer_name', 'invoice_order', 'remark'
        ];
        $re = yii::$app->db->createCommand()
            ->batchInsert(yii::$app->db->tablePrefix . "repo_fee", $keys, $data)
            ->execute();
        $sql = 'UPDATE jingsourcing_repo_fee set `pid`=`id` WHERE `pid`=0';
        yii::$app->db->createCommand($sql)->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    public function actionRepo_add()
    {
        $data = $_POST['data'];
        foreach ($data as $k => $v) {
            if (!$v[0]) {
                unset($data[$k]);
            }
        }
        $keys = [
            'date', 'name', 'product', 'product_number', 'package', 'carton',
            'pay', 'fee', 'pay_all', 'num', 'customer_name', 'invoice_order', 'remark'
        ];
        $re = yii::$app->db->
        createCommand()
            ->batchInsert(yii::$app->db->tablePrefix . "repo_fee", $keys, $data)
            ->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    /**打印费上传
     * @return bool
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws yii\db\Exception
     */
    public function actionPrint_upload_excel()
    {
        $file = $_FILES['print'];
        $suffix = explode('.', $file['name']);
        if (!($suffix[1] == 'xls' || $suffix[1] == 'xlsx')) {
            echo json_encode(['code' => 0, 'msg' => '不是excel文件']);
            return false;
        }

        $path = $this->get_folder();
        $real_path = $path . DIRECTORY_SEPARATOR . 'repo_' . time() . "." . $suffix[1];
        move_uploaded_file($file['tmp_name'], $real_path);

        //customer_list

        $objPHPExcel = $this->get_excel($real_path, $suffix[1]);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        for ($j = 2; $j <= $highestRow; $j++) {
            $date = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
            if ($date) {
                if (is_string($date)) {
                    $data[$j][] = $date;
                } else {
                    $data[$j][] = date('Y-m-d', ($objPHPExcel->getActiveSheet()->getCell("A$j")->getValue() - 25569) * 24 * 60 * 60);//date
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("F$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("G$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("H$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("I$j")->getValue();
                }
            }
        }
        $keys = ['date', 'product', 'num', 'price', 'fee', 'name', 'invoice_order', 'remark', 'group'];
        try {
            $re = yii::$app->db
                ->createCommand()
                ->batchInsert(yii::$app->db->tablePrefix . "print_fee", $keys, $data)
                ->execute();
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return json_encode(['code' => 0, 'msg' => $msg]);
        }

        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    public function actionPrint_add()
    {
        $data = $_POST['data'];
        foreach ($data as $k => $v) {
            if (!$v[0]) {
                unset($data[$k]);
            }
        }
        $keys = ['date', 'product', 'name', 'price', 'fee', 'num', 'invoice_order', 'remark', 'group'];
        $re = yii::$app->db
            ->createCommand()
            ->batchInsert(yii::$app->db->tablePrefix . "print_fee", $keys, $data)
            ->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    //客户收汇
    public function actionCustomer_earn()
    {
        $model = new \app\models\Staff();
        $list = $model->get_all_saleman();
        $saleman = [];
        foreach ($list as $k => $v) {
            $saleman[$v['job_number']] = $v['name'];
        }
        yii::$app->view->params['saleman'] = json_encode($saleman);
        return $this->render('customer_earn');
    }

    /**收款上传
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function actionReceipt_upload_excel()
    {
        $path = $this->get_folder();
        $file = $_FILES['file1'];
        $suffix = explode('.', $file['name']);
        $real_path = $path . DIRECTORY_SEPARATOR . "abcbank_" . time() . "." . $suffix[1];
        $re = move_uploaded_file($file['tmp_name'], $real_path);
        //获取客户list
        $model = new \app\models\Customer();
        $list = $model->get_all_customer();

        if ($re) {
            $objPHPExcel = $this->get_excel($real_path, $suffix[1]);
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            $highestColumn = $sheet->getHighestColumn();
            for ($j = 2; $j <= $highestRow; $j++) {
                $time = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
                if ($time) {
                    $data[$j][] = date('Y-m-d', ($objPHPExcel->getActiveSheet()->getCell("A$j")->getValue() - 25569) * 24 * 60 * 60);
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
                    $items = ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'O', 'P'];
                    $customer_name = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
                    $tmp = "";
                    if ($customer_name) {
                        foreach ($list as $key => $v) {
                            if (strtolower($v['customer_name']) == strtolower($customer_name)) {
                                $tmp = 1;
                                $data[$j][] = $v['number'];
                                $data[$j][] = $customer_name;
                                break;
                            }
                        }
                        if (!$tmp) {
                            $data[$j][] = "";
                            $data[$j][] = $customer_name;
                        }
                    } else {
                        $data[$j][] = "";
                        $data[$j][] = "";
                    }

                    foreach ($items as $v) {
                        $tmp = $objPHPExcel->getActiveSheet()->getCell("$v$j")->getValue();
                        if (0 === strpos($tmp, '=')) {
                            $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("$v$j")->getCalculatedValue();
                        } else {
                            $data[$j][] = $tmp;
                        }
                    }
                }
            }
            $db = new Income();
            $re = $db->insert_income($data);
            $db->save(['pid' => 'id'], ['pid' => 0]);
            if ($re) {
                $output = ['code' => 1, 'msg' => '添加成功'];
            } else {
                $output = ['code' => 0, 'msg' => '添加失败，请重试'];
            }
            echo json_encode($output);
        } else {
            echo json_encode(['code' => 0, 'msg' => '文件上传失败,请重试']);
        }
    }

    /**
     * 收款录入
     */
    public function actionInsert_income()
    {
        $data = yii::$app->request->post('data');
        foreach ($data as $k => $v) {
            if (empty($v['date'])) {
                unset($data[$k]);
            }
        }
        //获取当前余额信息
        //开始事务
        $tr = yii::$app->db->beginTransaction();
        try {
            $model = new AccountMoney();
            $db = new Income();

            $list = $model->all();
            foreach ($data as $v) {
                $pay = $v['total_fee'] + $v['in_come'] + $v['fee'];
                $money = $list[$v['pay_from']]['money'] + $pay;
                $list[$v['pay_from']]['money'] = $money;
                $model->save(['money' => $money], ['id' => $v['pay_from']]);
                $v['remain_money'] = $money;
                $db->insert($v);
            }
            $tr->commit();
            $output = ['code' => 1, 'msg' => '添加成功'];
        } catch (\Exception $e) {
            $tr->rollBack();
            $output = ['code' => 0, 'msg' => '添加失败，请重试'];
        }
        $sql = 'UPDATE jingsourcing_income SET pid=id WHERE pid=0';
        yii::$app->db->createCommand($sql)->execute();
        //结束事务
        echo json_encode($output);
    }

    /**
     * 农业银行表格
     */
    public function actionAbcbank()
    {
        $model = new \app\models\Staff();
        $list = $model->get_staff_list();
        $saleman = [];
        foreach ($list as $k => $v) {
            $saleman[$v['job_number']] = $v['name'];
        }
        $model = new Bank_bill();
        $list = $model->get_bank_type();
        $bank_type = [];
        foreach ($list as $v) {
            $bank_type[$v['id']] = $v['name'];
        }
        yii::$app->view->params['bank_type'] = json_encode($bank_type);
        yii::$app->view->params['saleman'] = json_encode($saleman);
        return $this->render('abcbank');
    }

    /**银行支出excel上传
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws yii\db\Exception
     */
    public function actionAbcbankupload_excel()
    {
        $path = $this->get_folder();
        $file = $_FILES['file1'];
        $suffix = explode('.', $file['name']);
        $real_path = $path . DIRECTORY_SEPARATOR . "abcbank_" . time() . "." . $suffix[1];
        $re = move_uploaded_file($file['tmp_name'], $real_path);
        //获取客户list
        $model = new \app\models\Customer();
        $list = $model->get_all_customer();
        if ($re) {
            $objPHPExcel = $this->get_excel($real_path, $suffix[1]);
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            try {
                for ($j = 2; $j <= $highestRow; $j++) {
                    $time = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
                    if ($time) {
                        $data[$j][] = date('Y-m-d', ($objPHPExcel->getActiveSheet()->getCell("A$j")->getValue() - 25569) * 24 * 60 * 60);
                        $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
                        $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("C$j")->getValue();
                        $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("D$j")->getCalculatedValue();
                        $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();
                        $customer_name = $objPHPExcel->getActiveSheet()->getCell("G$j")->getValue();
                        $tmp = "";
                        if ($customer_name) {
                            foreach ($list as $key => $v) {
                                if (strtolower($v['customer_name']) == strtolower($customer_name)) {
                                    $tmp = 1;
                                    $data[$j][] = $v['number'];
                                    $data[$j][] = $customer_name;
                                    break;
                                }
                            }
                            if (!$tmp) {
                                $data[$j][] = "";
                                $data[$j][] = $customer_name;
                            }
                        } else {
                            $data[$j][] = "";
                            $data[$j][] = "";
                        }

                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("H$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("I$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("J$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("K$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("L$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("M$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("N$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("O$j")->getValue();
                        $data[$j][] = (string)$objPHPExcel->getActiveSheet()->getCell("P$j")->getValue();

                    }
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                return json_encode(['code' => 0, 'msg' => $msg]);
            }

            try {
                $model = new Bank_bill();
                $re = $model->insert_bank_bill($data);
                $model->updatePid();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                return json_encode(['code' => 0, 'msg' => $msg]);
            }

            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '上传成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '文件上传失败,请重试']);
            }
        } else {
            echo json_encode(['code' => 0, 'msg' => '文件上传失败,请重试']);
        }
    }

    public function actionAbcbankupload()
    {
        $data = $_POST['data'];
        foreach ($data as $k => $v) {
            if (empty($v[0])) {
                unset($data[$k]);
            }
        }
        try {
            $model = new Bank_bill();
            $re = $model->insert_bank_bill($data);
            $model->updatePid();
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return json_encode(['code' => 0, 'msg' => $msg]);
        }
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => "添加成功"]);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    /**
     * 退税管理
     */
    public function actionRebate()
    {
        return $this->render('rebate');
    }

    public function actionRebate_upload()
    {
        $data = $_POST['data'];
        foreach ($data as $k => $v) {
            if (!$v[0]) {
                unset($data[$k]);
            }
        }
        $model = new \app\models\Rebate();
        if ($model->insert_rebate($data)) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }


    /**
     * 报销管理
     */
    public function actionReimburse()
    {
        $staffModel = new Staff();
        $p = is_numeric(yii::$app->request->get('page')) ? yii::$app->request->get('page') : 1;
        $s = is_numeric(yii::$app->request->get('per-page')) ? yii::$app->request->get('per-page') : 15;
        $where = [];

        $where['status'] = yii::$app->request->get('status');
        $where['type'] = yii::$app->request->get('type');
        $where['type2'] = yii::$app->request->get('type2');
        $where['invoice'] = trim(yii::$app->request->get('invoice'));
        $where['begin_date'] = yii::$app->request->get('begin_date');
        $where['end_date'] = yii::$app->request->get('end_date');
        if ($this->user['role'] == 10) {
            $where['job_number'] = $staffModel->getStaffByGroup(4);
        }
        $where['job_number'] = yii::$app->request->get('job_number');
        $model = new Baoxiao();
        $list = $model->all($where, ($p - 1) * $s, $s, ['create_at' => SORT_DESC]);
        $size = $model->get_baoxiao_size($where);
        if ($this->user['job_number'] == 999) {
            $where['current_verify'] = 2;
        } else {
            $where['current_verify'] = 1;
        }
        $page = new Pagination(['totalCount' => $size, 'pageSize' => $s]);
        //获取报销通过未打款数据最近2个月的
        $money = $model->sumMoney($where);
        $list2 = $model->all($where, ($p - 1) * $s, $s, ['create_at' => SORT_DESC]);
        $size2 = $model->get_baoxiao_size($where);

        $page2 = new Pagination(['totalCount' => $size2, 'pageSize' => $s]);

        $staff_list = $staffModel->staff_list();
        yii::$app->view->params['staff'] = $staff_list;
        yii::$app->view->params['list'] = $list;
        return $this->render('reimburse', ['money' => $money, 'page' => $page, 'page2' => $page2, 'list2' => $list2, 'size2' => $size2]);
    }

    /**报销状态修改
     * @return string
     */
    public function actionReimburse_edit()
    {
        if (yii::$app->request->isPost) {
            $id = (int)yii::$app->request->post('id');
            $status = (int)yii::$app->request->post('status');
            $data['status'] = $status;
            $data['verify_staff'] = $this->user['job_number'];
            $data['time'] = time();
            $model = new Baoxiao();
            $info = $model->one(['id' => $id]);
            BaseFunction::check_edit_auth($this->user['role']);
            if ($info['current_verify'] == 2) {
                if ($this->user['job_number'] != 999) {
                    return json_encode(['code' => 0, 'msg' => '已转交给管理,没有操作权限']);
                }
            }
            $re = $model->save($data, ['id' => $id]);
            if ($re) {
                return json_encode(['code' => 1, 'msg' => '修改成功']);
            } else {
                return json_encode(['code' => 1, 'msg' => '修改失败，请重试']);
            }
        }
    }

    public function actionReimburseUpdate()
    {
        if (yii::$app->request->isPost) {
            $id = yii::$app->request->post('id');
            $param = yii::$app->request->post('param');
            $value = trim(yii::$app->request->post('value'));
            $model = new Baoxiao();
            if ($model->save([$param => $value], ['id' => $id]) == 1) {
                echo json_encode(['code' => 1, 'msg' => '修改成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '修改失败']);
            }
        }
    }

    /**
     * 报销审核转交功能
     */
    public function actionUpdateVerifyStaff()
    {
        $id = yii::$app->request->post('id');
        if ($id) {
            $model = new Baoxiao();
            $res = $model->save(['current_verify' => 2], ['id' => $id]);
            if ($res) {
                return json_encode(['code' => 1, 'msg' => '更新成功']);
            } else {
                return json_encode(['code' => 0, 'msg' => '更新失败，请重试']);
            }
        }
    }

    /**
     *删除报销功能
     **/
    public function actionDelBaoxiao()
    {
        $id = yii::$app->request->post('id');

        if (yii::$app->request->isPost) {
            $re = yii::$app->db->createCommand()->delete(yii::$app->db->tablePrefix . 'baoxiao', ['id' => $id])->execute();
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '删除成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
            }
        }
    }

    public function actionUpdatePay()
    {
        if (yii::$app->request->isPost) {
            if ($this->user['role'] == 1 || $this->user['role'] == 2) {
                $model = new Baoxiao();
                $id = yii::$app->request->post('id');
                $info = $model->one(['id' => $id]);
                if ($info['status'] != 2) {
                    return json_encode(['code' => 0, 'msg' => '审核未通过，无法打款']);
                }
                $data['pay_staff'] = $this->user['job_number'];
                $data['pay_time'] = time();

                if ($model->save($data, ['id' => $id])) {
                    echo json_encode(['code' => 1, 'msg' => '更新成功']);
                } else {
                    echo json_encode(['code' => 0, 'msg' => '更新失败，请重试']);
                }
            }
        }
    }

    public function actionUpload_invoice_excel()
    {
        $file = ROOT_PATH . '/invoice.xlsx';
        $objPHPExcel = $this->get_excel($file, 'xlsx');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        $data = [];
        $model = new \app\models\Customer();
        $customer_list = $model->get_all_customer();
        $model = new \app\models\Staff();
        $staff_list = $model->get_all_saleman();
        for ($j = 2; $j <= $highestRow; $j++) {
            $saleman = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();
            if ($saleman) {
                $invoice = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();//invoice
                $data[$j][] = date('Y-m-d', strtotime('20' . substr($invoice, 2, 6)));//date
                $data[$j][] = $invoice;
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("A$j")->getCalculatedValue();//pi
                $name = $objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();
                $tmp = 0;
                foreach ($customer_list as $value) {
                    if ($value['customer_name'] == $name) {
                        $data[$j][] = $value['number'];
                        $data[$j][] = $value['customer_name'];
                        $tmp = 1;
                        break;
                    }
                }
                if ($tmp == 0) {
                    $data[$j][] = '';
                    $data[$j][] = $name;
                }
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("F$j")->getValue();//product
                $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("G$j")->getValue();//备注
                foreach ($staff_list as $k => $v) {
                    if ($v['name'] == $saleman) {
                        $data[$j][] = $saleman;
                        $data[$j][] = $v['job_number'];
                    }
                }
            } else {
                break;
            }
        }
        $keys = [
            'date', 'invoice', 'pi', 'customer_num', 'customer_name',
            'product', 'remark', 'saleman', 'saleman_num'
        ];
        $table = yii::$app->db->tablePrefix . 'invoice_new';
        yii::$app->db->createCommand()->batchInsert($table, $keys, $data)->execute();
    }

    //内陆费用
    public function actionInland()
    {
        $p = is_numeric(yii::$app->request->get('p')) ? yii::$app->request->get('p') : 1;
        $s = is_numeric(yii::$app->request->get('s')) ? yii::$app->request->get('s') : 15;
        $where = [];
        $where['saleman'] = yii::$app->request->get('saleman');
        $where['customer_name'] = trim(yii::$app->request->get('customer_name'));
        $where['customer_num'] = trim(yii::$app->request->get('customer_num'));
        $where['invoice'] = trim(yii::$app->request->get('invoice'));
        $where['order_num'] = trim(yii::$app->request->get('order_num'));
        $where['proxy_company'] = trim(yii::$app->request->get('proxy_company'));
        $model = new \app\models\Inland();
        $list = $model->get_inland_list($p, $s, $where);
        $staffModel = new \app\models\Staff();
        $staff_list = $staffModel->get_all_saleman();
        $staff = [];
        foreach ($staff_list as $v) {
            $staff[$v['job_number']] = $v;
        }

        foreach ($list as $k => $v) {
            $list[$k]['zh_name'] = isset($staff[$v['saleman']]['name']) ? $staff[$v['saleman']]['name'] : "";
        }
        $size = $model->get_inland_size();
        $model_factory = new ModelFactory();
        $proxy_company_list = $model_factory->get_proxy_company()->get_proxy_company();
        yii::$app->view->params['proxy_company_list'] = $proxy_company_list;
        yii::$app->view->params['staff_list'] = $staff_list;
        yii::$app->view->params['list'] = $list;
        return $this->render('inland');
    }

    function actionUpdate_inland()
    {
        if (yii::$app->request->isPost) {
            $id = yii::$app->request->post('id');
            $column = yii::$app->request->post('column');
            $value = yii::$app->request->post('value');
            $table = yii::$app->db->tablePrefix . 'inland_fee';
            $re = yii::$app->db->createCommand()->update($table, [$column => $value], ['id' => $id])->execute();
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '更新成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '更新失败，请重试']);
            }
        }
    }

    //上传内陆费excel
    public function actionUpload_inland_excel()
    {
        $file = $_FILES['file'];
        $re = \app\lib\BaseFunction::check_excel($file['type']);
        if (!$re) {
            echo json_encode(['code' => 0, 'msg' => '不是excel文件']);
            exit;
        }
        $suffix = explode('.', $file['name']);
        $move_path = $this->get_folder() . '/' . time() . substr(md5($file['name']), 0, 6) . '.' . $suffix[1];
        move_uploaded_file($file['tmp_name'], $move_path);

        $excel = $this->get_excel($move_path, $suffix[1]);
        $sheet = $excel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $data = [];
        for ($i = 2; $i < $highestRow; $i++) {
            $date = $excel->getActiveSheet()->getCell('A' . $i)->getValue();
            if (is_numeric($date)) {
                $date = date('Y-m-d', ($date - 25569) * 24 * 60 * 60);
            }
            if ($date) {
                $data[$i][] = $date;//出货日期
                $data[$i][] = $excel->getActiveSheet()->getCell('B' . $i)->getValue();//进仓编号
                $data[$i][] = $excel->getActiveSheet()->getCell('C' . $i)->getValue();//数量
                $data[$i][] = $excel->getActiveSheet()->getCell('D' . $i)->getValue();//体积
                $data[$i][] = $excel->getActiveSheet()->getCell('E' . $i)->getValue();//运费
                $data[$i][] = $excel->getActiveSheet()->getCell('F' . $i)->getValue();//进仓费
                $data[$i][] = $excel->getActiveSheet()->getCell('G' . $i)->getValue();//合计
                $data[$i][] = $excel->getActiveSheet()->getCell('H' . $i)->getValue();//客户编号
                $data[$i][] = $excel->getActiveSheet()->getCell('I' . $i)->getValue();//客户名
                $data[$i][] = $excel->getActiveSheet()->getCell('J' . $i)->getValue();//业务员
                $data[$i][] = $excel->getActiveSheet()->getCell('K' . $i)->getValue();//invoice
                $data[$i][] = $excel->getActiveSheet()->getCell('L' . $i)->getValue();//备注
            }
        }
        $table = yii::$app->db->tablePrefix . 'inland_fee';
        $keys = [
            'date', 'order_num', 'num', 'volume', 'ship_fee', 'in_repo_fee',
            'total_fee', 'customer_num', 'customer_name', 'saleman', 'invoice',
            'remark'
        ];
        $re = yii::$app->db->createCommand()->batchInsert($table, $keys, $data)->execute();
        $service = new InlandService();
        $service->updatePid();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
        }
    }

    //样品费

    public function actionSample()
    {
        return $this->render('sample');
    }

    public function actionUpload_sample_excel()
    {
        if (yii::$app->request->isPost) {
            $file = $_FILES['file'];
            if (!BaseFunction::check_excel($file['type'])) {
                echo json_encode(['code' => 0, 'msg' => '不是excel文件']);
                exit;
            }
            $suffix = explode('.', $file['name']);

            $file_path = $this->get_folder() . '/' . time()
                . substr(md5($file['name']), 0, 6) . '.' . $suffix[1];

            move_uploaded_file($file['tmp_name'], $file_path);

            //读取excel
            $excel = $this->get_excel($file_path, $suffix[1]);
            $sheet = $excel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            $data = [];
            for ($i = 2; $i < $highestRow; $i++) {
                //客户编号
                $customer_num = $excel->getActiveSheet()->getCell('B' . $i)->getValue();
                if ($customer_num) {
                    $data[][] = $customer_num;
                    $data[][] = $excel->getActiveSheet()->getCell('C' . $i)->getValue();//客户名称
                    $data[][] = $excel->getActiveSheet()->getCell('D' . $i)->getValue();
                    $data[][] = $excel->getActiveSheet()->getCell('E' . $i)->getValue();
                    $data[][] = $excel->getActiveSheet()->getCell('F' . $i)->getValue();
                    $data[][] = $excel->getActiveSheet()->getCell('G' . $i)->getValue();
                    $data[][] = $excel->getActiveSheet()->getCell('H' . $i)->getValue();
                    $data[][] = $excel->getActiveSheet()->getCell('I' . $i)->getValue();
                }
            }

            //插入数据库
            $table = yii::$app->db->tablePrefix . 'sample_fee';
            $keys = [
                'customer_num', 'customer_name', 'saleman', 'sample_fee',
                'sample_fee_out', 'exchange', 'remain', 'type'
            ];
            $re = YII::$app->db->createCommand()->batchInsert($table, $keys, $data);
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '添加成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => "添加失败，请重试"]);
            }
        }
    }

    public function actionPurchase_pay()
    {
        return $this->render('purchase_pay');
    }
}
